package com.yitiao.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 未知
 */
@RestController
@RequestMapping("/cache")
public class LocalCacheController {

    private volatile List<String> materialTypeList = new ArrayList<>();

    private ThreadLocal<List<String>> threadLocal = new ThreadLocal<>();

    ConcurrentMap<String,List<String>> map = new ConcurrentHashMap();

    @GetMapping("/add")
    public void add() throws InterruptedException {
        ArrayList<String> objects = new ArrayList<>();
        objects.add(Thread.currentThread().getName());
        materialTypeList.addAll(objects);
        Thread.sleep(2000);
        System.out.println(materialTypeList);
        threadLocal.set(materialTypeList);
        System.out.println(threadLocal.get());
    }

    @GetMapping("/clear")
    public void clear() throws InterruptedException {
        materialTypeList.add("new th");
    }

    private static volatile List<String> list = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            while (list.size()==0) {
//                System.out.println("list");
            }
            System.out.println("Thread 1: Flag is now true");
        });

        Thread thread2 = new Thread(() -> {
            list.add("1");
            System.out.println("Thread 2: Flag is set to true");
        });

        thread1.start();
        Thread.sleep(1000);
        thread2.start();
    }


}
